package com.i2works.smartluolong.api.controller.index;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.i2works.smartluolong.api.utils.BaseModule;
import com.i2works.smartluolong.utils.entity.ListParamsQuery;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.utils.plugin.Pager;
import com.i2works.smartluolong.web.entity.items.*;
import com.i2works.smartluolong.web.service.items.IItemsCategoryService;
import com.i2works.smartluolong.web.service.items.IItemsService;
import com.i2works.smartluolong.web.service.items.IMainItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping({"/interact"})
public class InteractController extends BaseModule
{
  private final IItemsCategoryService itemsCategoryService;
  private final IMainItemsService mainItemsService;
  private final IItemsService itemsService;

  @Autowired
  public InteractController(IItemsCategoryService itemsCategoryService, IMainItemsService mainItemsService, IItemsService itemsService)
  {
    this.itemsCategoryService = itemsCategoryService;
    this.mainItemsService = mainItemsService;
    this.itemsService = itemsService;
  }

  @GetMapping({ "/{page:[0-9]*}-{mainId:[0-9]*}"})
  public Result zwgk(ListParamsQuery params)
  {
    JSONObject json = new JSONObject();
    try
    {
      params.setCateId(21);
      ItemsCategoryQuery cateQuery = new ItemsCategoryQuery();
      cateQuery.setStatus(1);
      List cateList = this.itemsCategoryService.queryList(cateQuery);
      cateQuery.setId(params.getCateId());
      ItemsCategoryDO cate = (ItemsCategoryDO)this.itemsCategoryService.query(cateQuery);
      json.put("cateId", params.getCateId());
      json.put("cateTitle", cate.getTitle());
      json.put("cateList", cateList);
      json.put("mainId", params.getMainId());

      MainItemsQuery mainQuery = new MainItemsQuery();
      mainQuery.setCateId(params.getCateId());
      mainQuery.setStatus(1);
      List mainList = this.mainItemsService.queryList(mainQuery);
      json.put("mainList", mainList);

      //分页
      if (params.getPage() == null || params.getPage() == 0) {
        params.setPage(1);
      }
          ItemsQuery query = new ItemsQuery();
          List<ItemsDO> list = Lists.newArrayList();
          query.setCateId(params.getCateId());
          query.setStatus(1);
          if (params.getMainId() != null) {
            query.setMainId(params.getMainId());
          }
          Integer pageNumber = params.getPage();
          //查询总数量
          Integer queryCount = itemsService.queryCount(query);
      if (queryCount > 0) {
        query.setOffset(query.getLimit() * (pageNumber - 1));
        list = itemsService.queryList(query);
      }
      json.put("list", list);
      json.put("pager", new Pager(pageNumber, queryCount,10));
    } catch (ServiceException e) {
      logger.error("InteractController.zwgk政民互动列表异常：" + e.getMessage());
      return R.error("政民互动列表请求失败");
    }
    return R.success(json);
  }

  @GetMapping({"/detail/{id}"})
  public Result detail(@PathVariable("id") Integer id)
  {
    JSONObject json = new JSONObject();
    try
    {
      ItemsQuery query = new ItemsQuery();
      query.setId(id);
      query.setStatus(1);
      ItemsDO itemsDO = itemsService.query(query);
      json.put("itemsDO", itemsDO);
    } catch (ServiceException e) {
      logger.error("InteractController.detail政民互动列表详情页面异常：" + e.getMessage());
      return R.error("政民互动列表详情页面请求失败");
    }
    return R.success(json);
  }


}